import { storeToRefs } from 'pinia'
import emiter from './mitt'
import { useActiveObject } from '@renderer/stores/activeObject'
import { useHistory } from './useHistory'
import { useAddTools } from './useAddTools'
import { deleteDeviceAnnotation, updatePoint } from '@renderer/api'

const { deleteLabel } = useAddTools()
const activeObjectStore = useActiveObject()
const { activeShallowRef, activeVO } = storeToRefs(activeObjectStore)
const { commitHistory } = useHistory()
export default function useDelete() {
  async function deleteObject() {
    const Object3D = activeShallowRef.value
    emiter.emit('deleteObject', Object3D)
    if (Object3D) {
      if (Object3D.userData.type === 'roadway') {
        await updatePoint({
          tunnelName: Object3D.name,
          tunnelId: Object3D.uuid
        })
      }
      if (Object3D.userData.type === 'equipment') {
        await deleteDeviceAnnotation(Object3D.uuid)
      }
      activeObjectStore.setActiveVO(undefined)
      Object3D?.parent?.remove(Object3D)
      emiter.emit('viewChange')
      commitHistory(`删除物体${Object3D?.name}`, 'delete')
      if (Object3D.children) {
        deleteLabel(Object3D?.name)
      }
    }
  }
  return {
    deleteObject
  }
}
